/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/**
 *
 * @author Rossy
 */

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class JTextFieldFilter extends PlainDocument{

    private int iMaxLength = -1;
    public static final String LOWERCASE =
            "abcdefghijklmnopqrstuvwxyz";
    public static final String UPPERCASE =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String SYMBOLS =
            "? Â¿!$%&/()=^*[]_-:.;,><|@#~";
    public static final String ALPHA =
            LOWERCASE + UPPERCASE + SYMBOLS;
    public static final String NUMERIC =
            "0123456789-";
    public static final String FLOAT =
            NUMERIC + ".";
    public static final String ALPHA_NUMERIC =
            ALPHA + NUMERIC;
    protected String acceptedChars = null;
    protected boolean negativeAccepted = false;

    public JTextFieldFilter() {
        this(ALPHA_NUMERIC);
    }

    public JTextFieldFilter(String acceptedchars) {
        acceptedChars = acceptedchars;
    }

    public JTextFieldFilter(int maxlen) {
        iMaxLength = maxlen - 1;
        acceptedChars = ALPHA_NUMERIC;
    }

    public JTextFieldFilter(String acceptedchars, int maxlen) {
        acceptedChars = acceptedchars;
        iMaxLength = maxlen - 1;
    }

    public void setNegativeAccepted(boolean negativeaccepted) {
        if (acceptedChars.equals(NUMERIC)
                || acceptedChars.equals(FLOAT)
                || acceptedChars.equals(ALPHA_NUMERIC)) {
            negativeAccepted = negativeaccepted;
            acceptedChars += "-";
        }
    }

    private boolean verifyLen(String str) {
        if (iMaxLength < 0) { // aceptara cualquier num de caracteres
            return true;
        }
        int ilen = (getLength() + str.length());
        if (ilen <= (iMaxLength + 1)) { 
            return true; 
        } else {
            if (getLength() == iMaxLength) {
                return true; 
            } else {
                return false;
            }
        }
    }

    public void insertString(int offset, String str, AttributeSet attr)
            throws BadLocationException {
        if (str == null) {
            return;
        }
        if (verifyLen(str) == false) {
            return;
        }
        if (acceptedChars.equals(UPPERCASE)) {
            str = str.toUpperCase();
        } else if (acceptedChars.equals(LOWERCASE)) {
            str = str.toLowerCase();
        }

        for (int i = 0; i < str.length(); i++) {
            if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1) {
                return;
            }
        }

        if (acceptedChars.equals(FLOAT)
                || (acceptedChars.equals(FLOAT + "-") && negativeAccepted)) {
            if (str.indexOf(".") != -1) {
                if (getText(0, getLength()).indexOf(".") != -1) {
                    return;
                }
            }
        }

        if (negativeAccepted && str.indexOf("-") != -1) {
            if (str.indexOf("-") != 0 || offset != 0) {
                return;
            }
        }

        super.insertString(offset, str, attr);
    }
}
